package org.jeecg.modules.system.controller;


import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.cybermkd.mongo.kit.MongoQuery;
import com.cybermkd.mongo.kit.page.MongoPage;
import com.cybermkd.mongo.kit.page.MongoPaginate;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.system.entity.SysLog;
import org.jeecg.modules.system.entity.SysRole;
import org.jeecg.modules.system.service.ISysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 系统日志表 前端控制器
 * </p>
 *
 * @Author zhangweijian
 * @since 2018-12-26
 */
@RestController
@RequestMapping("/sys/log")
@Slf4j
public class SysLogController {

    @Autowired
    private ISysLogService sysLogService;

    /**
     * @param syslog
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     * @功能：查询日志记录
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<List> queryPageList(SysLog syslog, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        Result<List> result = new Result<>();
        MongoQuery query = new MongoQuery();
        //日志关键词
        String keyWord = req.getParameter("keyWord");
        if (oConvertUtils.isNotEmpty(keyWord)) {
            query.like("logContent", keyWord);
        }
        String operateType = req.getParameter("operateType");
        if (oConvertUtils.isNotEmpty(operateType)) {
            query.eq("operateType", oConvertUtils.getInt(operateType));
        }
        String createTime_begin = req.getParameter("createTime_begin");
        if (oConvertUtils.isNotEmpty(createTime_begin)) {
            String createTime_end = req.getParameter("createTime_end");
            query.gte("createTime", DateUtil.parse(createTime_begin)).lte("createTime", DateUtil.parse(createTime_end));
        }
        MongoPaginate page = new MongoPaginate(query.use("logs").eq("logType", syslog.getLogType()), pageSize, pageNo);
        MongoPage mongoPage = page.find(SysLog.class);
        log.info("查询当前页：" + mongoPage.getPage());
        log.info("查询当前页数量：" + mongoPage.getTotalPage());
        log.info("查询结果数量：" + mongoPage.getCount());
        log.info("数据总数：" + mongoPage.getTotalRow());
        result.setSuccess(true);
        result.setResult(mongoPage.getList());
        return result;
    }

    /**
     * @param id
     * @return
     * @功能：删除单个日志记录
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Result<SysLog> delete(@RequestParam(name = "id", required = true) String id) {
        Result<SysLog> result = new Result<SysLog>();
        MongoQuery query = new MongoQuery();
        JSONObject sysLog = query.byId("id").findOne();
        // SysLog sysLog = sysLogService.getById(id);
        if (sysLog == null) {
            result.error500("未找到对应实体");
        } else {
            long ok = query.use("logs").byId(id).delete();
            if (ok > 0) {
                result.success("删除成功!");
            }
        }
        return result;
    }

    /**
     * @param ids
     * @return
     * @功能：批量，全部清空日志记录
     */
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
    public Result<SysRole> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        Result<SysRole> result = new Result<SysRole>();
        if (ids == null || "".equals(ids.trim())) {
            result.error500("参数不识别！");
        } else {
            if ("allclear".equals(ids)) {
                this.sysLogService.removeAll();
                result.success("清除成功!");
            }
            this.sysLogService.removeByIds(Arrays.asList(ids.split(",")));
            result.success("删除成功!");
        }
        return result;
    }


}
